clear all

*Define global path for replication package
global path "~/Dropbox/IT_Revolution/Replication_package/JPE submission"

global path_rawdata "$path/Raw_data"
global path_cleandata "$path/Clean_data"
global path_output "$path/Output"

cap mkdir "$path_output"

*Step 1. Generate Figure 1
********************************************************************************

import delimited "$path_cleandata/early_manuf_emp.csv", clear

tempfile temp
save `temp'

import delimited "$path_cleandata/early_manuf_group_emp.csv", clear

merge 1:1 p using `temp', update
drop _m

gen transition = "manufacturing"

save "$path_cleandata/early_manuf_emp_all.dta", replace

import delimited "$path_cleandata/late_ict_emp.csv", clear

tempfile temp
save `temp'

import delimited "$path_cleandata/late_ict_group_emp.csv", clear

merge 1:1 p using `temp', update
drop _m

gen transition = "ict"

append using "$path_cleandata/early_manuf_emp_all.dta"


*** Employment

foreach var of varlist _all {
	rename `var' v_`var'
}
rename (v_p v_transition) (period transition)

reshape long v_, i(period transition) j(estimate) string

rename v_ value

gen group = "all" if inlist(estimate, "coef", "se", "dn", "up")
replace group = "young" if inlist(estimate, "coef1", "se1", "dn1", "up1")
replace group = "old" if inlist(estimate, "coef2", "se2", "dn2", "up2")

replace estimate = subinstr(estimate,"1","",.)
replace estimate = subinstr(estimate,"2","",.)
drop if estimate == "se"

reshape wide value, i(estimate transition group) j(period)

gen value0 = 0
order transition estimate group value0 value1 value2 value3 value4

label variable value0 "t{subscript:0}"
label variable value1 "t{subscript:0}+10"
label variable value2 "t{subscript:0}+20"
label variable value3 "t{subscript:0}+30"
label variable value4 "t{subscript:0}+40"

sort transition group estimate

* All workers
mkmat value* if group == "all" & transition == "manufacturing", matrix(Manufacturing)
mkmat value* if group == "all" & transition == "ict", matrix(ICT)

coefplot (matrix(ICT), offset(-0.075) mcolor(black) ciopts(lcolor(black))) (matrix(Manufacturing), offset(0.075) msymbol(D) mcolor(gray) ciopts(lcolor(gray))), ci((2 3)) yline(0, lc(black)) vert title("Panel A: Relative Employment", size(medium) color(black)) yscale(range(-0.05 0.35)) ylabel(0 0.1 0.2 0.3) legend(size(small) cols(3) colgap(2) keygap(1) order(- "Occupation exposure to innovations in:" 2 "ICT" 4 "Manufacturing")) graphregion(color(white))
graph save "$path_cleandata/fig1_all", replace

* Old workers
mkmat value* if group == "old" & transition == "manufacturing", matrix(est_manuf_old)
mkmat value* if group == "old" & transition == "ict", matrix(est_ict_old)

coefplot (matrix(est_ict_old), offset(-0.075) mcolor(black) ciopts(lcolor(black))) (matrix(est_manuf_old), offset(0.075) msymbol(D) mcolor(gray) ciopts(lcolor(gray))), ci((2 3)) yline(0, lc(black)) vert title("Panel C: Relative Employment, Older Workers", size(medium) color(black)) yscale(range(-0.05 0.35)) ylabel(0 0.1 0.2 0.3) legend(off) graphregion(color(white))
graph save "$path_cleandata/fig1_old", replace

* Young workers
mkmat value* if group == "young" & transition == "manufacturing", matrix(est_manuf_young)
mkmat value* if group == "young" & transition == "ict", matrix(est_ict_young)

coefplot (matrix(est_ict_young), offset(-0.075) mcolor(black) ciopts(lcolor(black))) (matrix(est_manuf_young), offset(0.075) msymbol(D) mcolor(gray) ciopts(lcolor(gray))) , ci((2 3)) yline(0, lc(black)) vert title("Panel D: Relative Employment, Younger Workers", size(medium) color(black)) yscale(range(-0.05 0.35)) ylabel(0 0.1 0.2 0.3) legend(off) graphregion(color(white))
graph save "$path_cleandata/fig1_young", replace

*** Wage

import delimited "$path_cleandata/late_ict_inc.csv", clear

tempfile temp
save `temp'

import delimited "$path_cleandata/late_ict_group_inc.csv", clear

merge 1:1 p using `temp', update
drop _m

foreach var of varlist _all {
	rename `var' v_`var'
}
rename v_p period

reshape long v_, i(period) j(estimate) string

rename v_ value

keep if inlist(estimate, "coef", "dn", "up")

reshape wide value, i(estimate) j(period)

gen value0 = 0
order estimate value0 value1 value2 value3 value4

label variable value0 "t{subscript:0}"
label variable value1 "t{subscript:0}+10"
label variable value2 "t{subscript:0}+20"
label variable value3 "t{subscript:0}+30"
label variable value4 "t{subscript:0}+40"

mkmat value*, matrix(est_ict_wage)
coefplot (matrix(est_ict_wage), mcolor(black) ciopts(lcolor(black))), ci((2 3)) yline(0, lc(black)) vert title("Panel B: Relative Wages", size(medium) color(black)) ylabel(-0.06(0.03)0.06) legend(off) graphregion(color(white))
graph save "$path_cleandata/fig1_inc", replace

*** Combine graphs

grc1leg "$path_cleandata/fig1_all" "$path_cleandata/fig1_inc" "$path_cleandata/fig1_old" "$path_cleandata/fig1_young", legendfrom("$path_cleandata/fig1_all") graphregion(color(white))
graph export "$path_output/Fig1_final.png", as(png) replace
graph export "$path_output/Fig1_final.eps", as(eps) replace

erase "$path_cleandata/early_manuf_emp_all.dta"
erase "$path_cleandata/fig1_all.gph" 
erase "$path_cleandata/fig1_inc.gph" 
erase "$path_cleandata/fig1_old.gph" 
erase "$path_cleandata/fig1_young.gph"

*Step 2. Generate Figure A3
********************************************************************************

import delimited "$path_cleandata/FigA3_multiplecutoff_early.csv", clear

gen transition = "Manufacturing"

tempfile temp
save `temp'

import delimited "$path_cleandata/FigA3_multiplecutoff_late.csv", clear

gen transition = "ICT"

append using `temp'

rename (coef up_sig90 dn_sig90) (v_coef v_up_sig90 v_dn_sig90)

reshape long v_, i(transition p) j(estimate) string

rename v_ value

drop se

reshape wide value, i(estimate transition) j(p)

sort transition estimate

forvalues y = 1(1)11 {
	local label=24+`y'
	label variable value`y' "`label'"
}

mkmat value* if transition == "Manufacturing", matrix(est_cutoff_manuf)
mkmat value* if transition == "ICT", matrix(est_cutoff_ict)

coefplot (matrix(est_cutoff_manuf), msymbol(D) offset(0.075) mcolor(gray) ciopts(lcolor(gray))) (matrix(est_cutoff_ict), offset(-0.075) mcolor(black) ciopts(lcolor(black))), ci((2 3)) yline(0, lc(balck)) yscale(range(-0.1 0.16)) ylabel(-.1(0.05)0.15) xtitle("Maximum Age of Workers in Younger Generation") legend(size(medium) cols(3) colgap(2) keygap(1) order(- "Occupation exposure to innovations in:" 4 "ICT" 2 "Manufacturing")) vert graphregion(color(white))
graph export "$path_output/FigA3_mult_cutoff_age.png", replace
graph export "$path_output/FigA3_mult_cutoff_age.eps", replace

*Step 3. Generate Figure A1
********************************************************************************

*3.1. 1900 histogram of occupations (use "occ", not "occ1990dd" as there is no occupation description for occ1990dd)

use "$path_cleandata/data_occ_1900_1940_s1_a29distance.dta", clear
keep if group ==0
egen tot = total(emp1900)
gen percent = emp1900/tot*100
gsort -percent
keep occ percent emp1900 tot
keep if _n<51

decode occ, gen(occ1)
replace occ1 = strproper(occ1)
gen occ1_1 = subinstr(occ1,"N.E.C","nec",.)
gen occ1_2 = subinstr(occ1_1,"Nec.","nec",.)
gen occ1_3 = subinstr(occ1_2,"Nec","nec",.)
gen occ1_4 = subinstr(occ1_3,"nec.","nec",.)

graph bar percent, over(occ1_4, sort(1) descending label(angle(45) labsize(tiny))) ytitle("Percentage") graphregion(fcolor(white)) xsize(10)
graph export "$path_output/occupation_percentage_1900.png", as(png) replace
graph export "$path_output/occupation_percentage_1900.eps", as(eps) replace


*3.2. 1980 histogram of occupations 

use "$path_cleandata/data_occ_1980_2010_s1_a29distance.dta", clear

merge m:1 occ1990dd using "$path_rawdata/occ1990dd_desciption.dta"
drop if _m<3

keep if group ==0
egen tot = total(emp1980)
gen percent = emp1980/tot*100
gsort -percent
keep occ1990dd occ percent emp1980 tot
keep if _n<51

gen occ1_1 = subinstr(occ,", n.e.c."," (nec)",.)
graph bar percent, over(occ1_1, sort(1) descending label(angle(45) labsize(tiny))) ytitle("Percentage") graphregion(fcolor(white)) xsize(10)
graph export "$path_output/occupation_percentage_1980.png", as(png) replace
graph export "$path_output/occupation_percentage_1980.eps", as(eps) replace

